<template>
  <div>
    <Modal v-model="show" :title="title" :mask-closable="false" :closable="false">
      <Form ref="editModalForm" :model="editUser" :rules="ruls" :label-width="80">
        <!-- <FormItem label="ID">
          <Input disabled v-model.trim="user.userNo"></Input>
        </FormItem>-->
        <FormItem label="用户编号" prop="id">
          <Input disabled v-model.trim="editUser.id"></Input>
        </FormItem>
        <FormItem label="用户姓名" prop="name">
          <Input v-model.trim="editUser.name"></Input>
        </FormItem>
        <FormItem label="角色组">{{ getRoleName(editUser)}}</FormItem>
      </Form>
      <div slot="footer">
        <Button type="default" :disabled="cancelDisabled" @click="cancel(false)">取消</Button>
        <Button type="primary":loading="loading"  @click="ok()">确定</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
export default {
  data() {
    return {
      show: true,
      editUser: {},
      cancelDisabled:false,
      loading:false,
      ruls: {
        name: [
          { required: true, message: "请填写姓名！" },
          { max: 10, message: "长度不能超过10" }
        ]
      }
    };
  },
  props: {
    user: {
      type: Object,
      default: {}
    },title:{
       type: String,
      default: "更新用户"
    }
  },
  created() {
    this.editUser = Object.assign({}, this.user);
  },
  methods: {
    cancel(reload = false) {
      this.$emit("cancel", "edit", reload);
    },
    getValidate() {},
    ok() {
      this.$refs.editModalForm.validate(valid => {
        if (valid) {
          this.edit();
        }
      });
    },
    edit() {
      this.loading = true;
      
      var url = "/user/edit";
      this.post(url, this.editUser, {}).then(res => {
         this.loading = false;
        if (this.checkIsSsuccess(res)) {
          this.cancelDisabled = true;
          this.$Message.success({
            content: "修改成功！",
            duration: 3,
            closable: true,
            onClose: () => {
              this.$emit("success",this.editUser);
              this.cancel(false);
            }
          });
        } else {
          this.$Message.error({
            content: res["msg"],
            duration: 0,
            closable: true
          });
        }
      });
    },getRoleName(row){
      if(row.rolesNames){
        let name = "";
        row.rolesNames.forEach(k=>
        {
          name +=k+",";
          
        });
        name = name.substring(0,name.length-1);
        return name;
      }
      return "";
    }
  }
};
</script>

